package com.etbsms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.etbsms.dao.IRecNumberDAO;
import com.etbsms.domain.RecNumber;
import com.etbsms.service.IRecNumberService;

@Service
@Component("recNumberService")
public class RecNumberServiceImpl implements IRecNumberService{
	
	@Autowired
	private IRecNumberDAO recNumberDAO;	

	@Override
	public boolean save(RecNumber entity) {
		// TODO Auto-generated method stub
		return recNumberDAO.save(entity);
	}

	@Override
	public String getNextRecNumber() {		
		RecNumber rNumber = recNumberDAO.get(1);
		int number = rNumber.getNumber() + 1;
		rNumber.setNumber(number);	
		return recNumberDAO.update(rNumber) ? recNumber(number) : null;
	}

	@Override
	public Integer get() {
		// TODO Auto-generated method stub
		return recNumberDAO.get(1).getNumber();
	}
	
	private static String recNumber(int n){
		String _n = String.valueOf(n);
		for(int i=_n.length();i<6;i++)
			_n = "0" + _n;		
		return _n;		
	}
}
